package com.td.client.gui;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Vector2f;

public class TDAnimation {

	SpriteSheet sheet;
	org.newdawn.slick.Animation[] animation;
	float size;
	Vector2f direction = new Vector2f();
	public TDAnimation(Image image, int duration, float size)
	{
		sheet = new SpriteSheet(image, (int) size, (int) size);
		animation = new Animation[sheet.getVerticalCount()];
		for(int i = 0 ; i < animation.length ; i++)
			animation[i] = new Animation(sheet, 0, i, sheet.getHorizontalCount() - 1, i, true, duration, false);
	
	}
	public void draw(float x, float y)
	{
		calculateAnimation().draw(x - size/2, y - size/2);
	}
	public void setDirection(Vector2f direction)
	{
		this.direction = direction;
	}
	public void update()
	{		
		for(Animation ani : animation)
			ani.update(10);
	}
	public boolean isEnd()
	{
		Animation a = calculateAnimation();
		if(a.getFrame() == a.getFrameCount() - 1)
		{
			for(Animation ani : animation)
				ani.setCurrentFrame(0);
			return true;
		}
		return false;			
	}
	public void start()
	{
		calculateAnimation().start();
	}
	public void stop()
	{
		calculateAnimation().stop();
	}
	private Animation calculateAnimation() {
		double theta = direction.getTheta();
        int frame = ((int)Math.round(36 * theta / 360) + 9) % 36;
        return animation[frame];
	}
}
